package com.eascs.ticket.ticket.service;

import java.util.Date;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.ticket.common.entity.EntityUtil;
import com.eascs.ticket.common.service.BaseSevice;
import com.eascs.ticket.ticket.entity.TicketTimeLog;

@Service
public class TicketTimeLogService extends BaseSevice {

	@Transactional(transactionManager = "ticketTransactionManager")
	public TicketTimeLog insertTicketTimeLog(String ticketId, String operator, Date startDate) {
		TicketTimeLog ticketTimeLog = this.getTicketTimeLog(ticketId, operator);
		if (null != ticketTimeLog)
			return ticketTimeLog;
		ticketTimeLog = new TicketTimeLog();
		ticketTimeLog.setTicketId(ticketId);
		ticketTimeLog.setOperator(operator);
		ticketTimeLog.setStartTime(startDate);
		EntityUtil.initInsertEntity(ticketTimeLog);
		this.insertTicketTimeLog(ticketTimeLog);
		return ticketTimeLog;
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public TicketTimeLog updateTicketTimeLog(String ticketId, String operator, Date endDate) {
		TicketTimeLog ticketTimeLog = this.getTicketTimeLog(ticketId, operator);
		if (null == ticketTimeLog) {
			ticketTimeLog = this.insertTicketTimeLog(ticketId, operator, endDate);
		}
		ticketTimeLog.setEndTime(endDate);
		EntityUtil.initUpdateEntity(ticketTimeLog);
		this.proxyDao.updateById(ticketTimeLog);
		return ticketTimeLog;
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void insertTicketTimeLog(TicketTimeLog ticketTimeLog) {
		this.proxyDao.insert(ticketTimeLog);
	}

	public TicketTimeLog getTicketTimeLog(String ticketId, String operator) {
		TicketTimeLog ticketTimeLog = new TicketTimeLog();
		ticketTimeLog.setTicketId(ticketId);
		ticketTimeLog.setOperator(operator);
		return this.proxyDao.findEntityWithAny(ticketTimeLog);
	}
}
